#ifndef _TM_H_
#define _TM_H_
#include "define.h"

//#define		BUZZER_ON()				{_pc0=1;}
//#define		BUZZER_OFF()			{_pc0=0;}

#define		CTM0_ISR_ON()			{_mf6e=1;_ctm0ae=1;_mf6f=0;_ctm0af=0;}
#define		CTM0_ISR_OFF()			{_ctm0ae=0;_mf6f=0;_ctm0af=0;}
#define		CTM0_ON()				{_ct0on=1;}
#define		CTM0_OFF()				{_ct0on=0;}

#define		CTM1_ISR_ON()			{_mf7e=1;_ctm1ae=1;_mf7f=0;_ctm1af=0;}
#define		CTM1_ISR_OFF()			{_ctm1ae=0;_mf7f=0;_ctm1af=0;}
#define		CTM1_ON()				{_ct1on=1;}
#define		CTM1_OFF()				{_ct1on=0;}

#define		CTM2_ISR_ON()			{_mf8e=1;_ctm2ae=1;_mf8f=0;_ctm2af=0;}
#define		CTM2_ISR_OFF()			{_ctm2ae=0;_mf8f=0;_ctm2af=0;}
#define		CTM2_ON()				{_ct2on=1;}
#define		CTM2_OFF()				{_ct2on=0;}

#define	TB0_ON()					{_tb0on=1;}
#define	TB0_OFF()					{_tb0on=0;}

#define TMC_ON()                  	{_tmcon = 1;}
#define TMC_OFF()					{_tmcon = 0;}

//CLR TMCAF,TMCPF
#define CLR_TMC_TMCAPF()			{_tmcaf = 0;_tmcpf = 0;}
#define CLR_TMC_TMCAF()				{_tmcaf = 0;}
#define CLR_TMC_TMCPF()				{_tmcpf = 0;}


#define TMC_Fsys_div4()				{_tmcck2 = 0;_tmcck1 = 0;_tmcck0 = 0;}
#define TMC_Fsys_div1()				{_tmcck2 = 0;_tmcck1 = 0;_tmcck0 = 1;}
#define TMC_Fh_div16()				{_tmcck2 = 0;_tmcck1 = 1;_tmcck0 = 0;}
#define TMC_Fh_div64()				{_tmcck2 = 0;_tmcck1 = 1;_tmcck0 = 1;}
#define TMC_Fsub()					{_tmcck2 = 1;_tmcck1 = 0;_tmcck0 = 0;}
//#define TMC_Fsub()				{_tmcck2 = 1;_tmcck1 = 0;_tmcck0 = 1;}
#define TMC_TMCCKO_R_Clk()			{_tmcck2 = 1;_tmcck1 = 1;_tmcck0 = 0;}
#define TMC_TMCCKO_F_Clk()			{_tmcck2 = 1;_tmcck1 = 1;_tmcck0 = 1;}

#define TMC_STC_8192()				{_stc2 = 0;_stc1 = 0;_stc0 = 0;}			//0.25ms
#define TMC_STC_8192_2()			{_stc2 = 0;_stc1 = 0;_stc0 = 1;}			//0.5ms
#define TMC_STC_8192_4()			{_stc2 = 0;_stc1 = 1;_stc0 = 0;}			//1ms
#define TMC_STC_8192_8()			{_stc2 = 0;_stc1 = 1;_stc0 = 1;}			//2ms
#define TMC_STC_8192_16()			{_stc2 = 1;_stc1 = 0;_stc0 = 0;}			//4ms
#define TMC_STC_8192_32()			{_stc2 = 1;_stc1 = 0;_stc0 = 1;}			//8ms
#define TMC_STC_8192_64()			{_stc2 = 1;_stc1 = 1;_stc0 = 0;}			//16ms
#define TMC_STC_8192_128()			{_stc2 = 1;_stc1 = 1;_stc0 = 1;}			//32ms

#define TMC_Capture_Mode()			{_tmcm1 = 0;_tmcm0 = 0;}
#define TMC_Timer_Counter_Mode()	{_tmcm1 = 1;_tmcm0 = 0;}

#define TMC_Capture_Rise()			{_tmcio1 = 0;_tmcio0 = 0;}
#define TMC_Capture_Fall()			{_tmcio1 = 0;_tmcio0 = 1;}
#define TMC_Capture_Both()			{_tmcio1 = 1;_tmcio0 = 0;}
#define TMC_Capture_Dis()			{_tmcio1 = 1;_tmcio0 = 1;}












typedef struct
{      
  u8	Fg_1ms  	:1;
  u8	Fg_10ms  	:1;
  u8	Fg_100ms  	:1;
  u8	Fg_1s  		:1;
  u8	Fg_1min  	:1;
  u8	Fg_1h  		:1;
  u8	Fg_1day  	:1;
  u8	Fg_1mon		:1;
}_Time_Flag;    

typedef struct
{      
  u8	Time_1ms  	;
  u8	Time_10ms  	;
  u8	Time_100ms  ;
  u8	Time_1s  	;
  u8	Time_1min  	;
  u8	Time_1h  	;
}_Time_State;




extern _Time_Flag 		Time_Flag;
extern _Time_State	 	Time_State;
 extern volatile bit B_FLAG_PotDet;
//extern volatile bit		B_FLAG_PowerStable1, B_FLAG_PowerStable2,B_FLAG_PotDet,B_FLAG_PotDet2;
//extern volatile u8		PowerStableCnt;

unsigned int fun_TM_Capture_Num();
unsigned int fun_TM_Capture_Num1();

//void TM_Conter_Init(void);
void fun_TM_Conter(void);
void CTM0_Init(void);
void Time_Base(void);
void CTM1_Init(void);		
void CTM2_Init(void);
void TB0_Init(void);
void CTM_Init(void);
void PTM_Init(void);

#endif
             
